﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Web.Support;
using SIPO.Library.DomainObject.Administration.UserManagement;

namespace SIPO.Web.Forms.Transaction
{
    public partial class recDemandform : SIPO.Web.BasePage
    {
        int id = 0;
        string aksi = string.Empty;

        protected override void OnLoad(EventArgs e)
        {
           if(Request.Params["DemandNo"] != null){
               id = int.Parse(Request.Params["DemandNo"].ToString());
           }

           if (Request.Params["aksi"] != null)
           {
               aksi = Request.Params["aksi"].ToString();
           }
            
            if (!IsPostBack)
            {
                recActionDemandRepo recActionDemandDal = new recActionDemandRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recDemandRepo recDemandDal = new recDemandRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recDemand demands = recDemandDal.GetListByDemandNo(id);
                List<recActionDemand> recActionDemands = null;

                if (demands.recDemandDetail.StatusID == 0)
                {
                    recActionDemands = recActionDemandDal.GetList();
                }
                else
                {
                    if (aksi == "insert")
                    {
                        recActionDemands = recDemandDal.GetListActionDemandByActionId(id);
                    }
                    else
                    {
                        recActionDemands = recActionDemandDal.GetList();
                    }
                }

                ddlUpayaPenagihan.Items.Clear();

                recActionDemands.ForEach(delegate(recActionDemand actionDemand)
                {
                    ddlUpayaPenagihan.Items.Add(new ListItem(actionDemand.ActionName, actionDemand.ActionID.ToString()));
                });
                ddlUpayaPenagihan.Items.Insert(0, new ListItem("-- pilih upaya penagihan  --", string.Empty));
                
                //jika melakukan edit data
                if (id > 0)
                {
                    hfTransTypeID.Value = demands.recDemandDetail.TransTypeID.ToString();
                    hfTransName.Value = demands.recDemandDetail.TransName.ToString();
                    txtNamaPerusahaan.Text = demands.DebtorName;
                    hfEmail.Value = demands.recDebtor.Email;
                    txtJenisPungutan.Text = demands.recDemandDetail.TransName;
                    txtTglJatuhTempo.Text = demands.recDemandDetail.DueDate.ToString("dd/MM/yyyy");
                    txtTglSurat.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    txtNoSurat.Text = demands.recDemandDetail.LatterNo.ToString();
                    
                    

                    //if (aksi.Equals("insert"))
                    //{
                    //    if (demands.recDemandDetail.StatusID == 1)
                    //    {
                    //        ddlUpayaPenagihan.Items.FindByValue("2").Selected = true;
                    //    }
                    //    else if (demands.recDemandDetail.StatusID == 2)
                    //    {
                    //        ddlUpayaPenagihan.Items.FindByValue("3").Selected = true;
                    //    }
                    //    else
                    //    {
                    //        ddlUpayaPenagihan.Items.FindByValue("1").Selected = true;
                    //    }
                    //}
                    //else
                    //{
                    //    if (demands.recDemandDetail.StatusID > 0)
                    //    {
                    //        ddlUpayaPenagihan.Items.FindByValue(demands.recDemandDetail.StatusID.ToString()).Selected = true;
                    //    }

                    //    txtTglSurat.Text = string.Empty;
                    //}

                    if (aksi.Equals("update"))
                    {
                        if (demands.recDemandDetail.StatusID > 0)
                        {
                            ddlUpayaPenagihan.Items.FindByValue(demands.recDemandDetail.StatusID.ToString()).Selected = true;
                            txtTglAkhirPeneguran.Text = demands.recDemandDetail.DueDateLetter.Value.ToString("dd/MM/yyyy");
                        }

                        
                        //txtNoSurat.Text = demands.recDemandDetail.LatterNo.ToString();
                    }
                    else
                    {
                        
                        txtNoSurat.Text = string.Empty;
                        txtTglSurat.Text = string.Empty;
                    }
                }

                hfDueDateLetter.Value = demands.recDemandDetail.DueDateLetter == null ? string.Empty : demands.recDemandDetail.DueDateLetter.Value.ToString("dd/MM/yyyy");
                hfDemandNo.Value = id.ToString();
                hfAksi.Value = aksi;
                hfTransNo.Value = demands.recDemandDetail.TransNo == null ? string.Empty : demands.recDemandDetail.TransNo.ToString();
                hfStatusId.Value = demands.recDemandDetail.StatusID.ToString();
                //GetPrivilege();    
            }
        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').hide();", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
    }
}